package com.hktf.moedokilock.widget;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.PowerManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextClock;

import com.hktf.corelib.BaseApplication;
import com.hktf.corelib.constant.KEYS;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.moedokilock.R;
import com.hktf.uilib.utils.DisplayUtil;
import com.hktf.uilib.utils.ImageUtilNew;
import com.hktf.uilib.utils.LockScreenUtil;
import com.hktf.uilib.widget.SlideCloseFrameLayout;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * author  cdz
 * date  2025/3/22.
 * desc:
 */

public class FloatingLockscreen extends LinearLayout{
    private WindowManager windowManager;
    private WindowManager.LayoutParams params;

    public FloatingLockscreen(Context context) {
        super(context);

        // 设置悬浮按钮的图标
//        setImageResource(R.drawable.icon24_freeze_normal);

        // 创建WindowManager和LayoutParams对象
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        params = new WindowManager.LayoutParams(-1, -1, Build.VERSION.SDK_INT < 26 ? 2010 : 2038, 38274817, -3);
        int layoutType;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutType = WindowManager.LayoutParams.TYPE_PHONE;
        }
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                layoutType,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
                        WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                        WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT
        );
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P ) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ) {
                params.layoutInDisplayCutoutMode =WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
            }else {
                params.layoutInDisplayCutoutMode =WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            }
        }

        //            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        //                WindowInsetsController controller = getWindowInsetsController();
        //                if (controller != null) {
        //                    //不显示系统导航栏和状态栏
        //                    controller.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
        //                    // 设置沉浸式粘性模式，用户交互后系统栏短暂显示后再次隐藏
        //                    controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
        //                }
        //            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //                // Android 4.4 到 Android 10
        //                int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
        //                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        //                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        //                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        //                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        //                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        //                setSystemUiVisibility(uiOptions);
        //            }
        //不显示系统导航栏和状态栏 这段代码在安卓11及以上手机也能用
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        setSystemUiVisibility(uiOptions);
//        setOnApplyWindowInsetsListener((v, insets) -> {
//            int statusBarHeight = insets.getSystemWindowInsetTop();
//            int navBarHeight = insets.getSystemWindowInsetBottom();
//            v.setPadding(0, statusBarHeight, 0, navBarHeight);
//            // 设置状态栏和导航栏背景
//            //                v.setBackgroundColor(statusBarColor);
//            return insets;
//        });



        // 设置悬浮按钮的显示位置
        params.gravity = Gravity.TOP | Gravity.START;
//        params.x = 0;
//        params.x = 0;
//        params.y = 0;

        initView(context);
        initData();
    }
    private ImageView iv;
    private TextClock tvTime;
    private android.widget.TextView tvDate;
    private android.widget.TextView tv3;


    private void initView(Context context){
        SlideCloseFrameLayout rootView = (SlideCloseFrameLayout) View.inflate(context, R.layout.act_wallpaper, null);

        rootView.setOnSlideCloseListener(new SlideCloseFrameLayout.OnSlideCloseListener() {
            @Override
            public void onStartSlide() {

            }

            @Override
            public void onStopSlide(boolean isClose) {
                if (isClose) {
                    hide();
                }

            }
        });
        Point screenSize = new Point();
        windowManager.getDefaultDisplay().getRealSize(screenSize);


        iv = rootView.findViewById(R.id.iv);
        tvTime = rootView.findViewById(R.id.tv_time);
        tvDate = rootView.findViewById(R.id.tv_date);
        tv3 = rootView.findViewById(R.id.tv3);
        FrameLayout.LayoutParams params= (FrameLayout.LayoutParams) iv.getLayoutParams();
        params.width=screenSize.x;
        params.height=screenSize.y;
        iv.setLayoutParams(params);
        iv.requestLayout();
        addView(rootView);
    }

    private void initData() {
        LockScreenUtil.initLock(getContext(),iv,tvTime,tvDate);

    }


    private PowerManager.WakeLock wakeLock;
    public boolean isShow;

    // 显示悬浮按钮
    public void show() {
        try {
            windowManager.addView(this, params);




            // 获取电源管理器
            PowerManager powerManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);

            if (wakeLock == null) {
                // 创建唤醒锁
                wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "FloatingActionButton:WakeLock");
            }
            // 获取唤醒锁并保持屏幕唤醒状态
            wakeLock.acquire();
            isShow=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 显示悬浮按钮
    public void setLayout(int x,int y) {
        params.x=x;
        params.y=y;
        windowManager.updateViewLayout(this, params);
    }

    // 隐藏悬浮按钮
    public void hide() {
        windowManager.removeView(this);
        // 释放唤醒锁
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
        isShow=false;
    }
}
